package com.example.init.controller;

import com.example.init.Person;
import com.example.init.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

//    @Qualifier("person2")
//    @Autowired

    //使用 @Resource 注解指定 bean
    @Resource(name = "person2")
    private Person person2;


    private UserService userService;
    /**
     * setter 方法注入
     */
    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    /**
     * 构造方法注入
     * @param username
     * @param password
     * @param session
     * @return
     */
//    @Autowired
//    public UserController(UserService userService) {
//        this.userService = userService;
//    }
//    public UserController() {
//    }


    /**
     * 属性注入
     * @param username
     * @param password
     * @param session
     * @return
     */
//    @Autowired
//    private UserService userService;



    @RequestMapping("/login")
    public Boolean login(String username,String password,HttpSession session){
        if(!StringUtils.hasLength(username) || !StringUtils.hasLength(password)){
            return false;
        }

        if("admin".equals(username) && "admin".equals(password)){
            //设置登录用户 Session 信息
            session.setAttribute("username","admin");
            return true;
        }
        return false;
    }

    @RequestMapping("/getUserInfo")
    public String getUser(HttpServletRequest request){
        HttpSession session = request.getSession(false);
        return (String) session.getAttribute("username");
    }
}
